<?php
use application\utilities\Dev as Dev;

/**
 * Pointer to the database object
 * 
 * @package Concerto
 * @subpackage Helpers
 * @see \application\system\DB
 */
function DB() {
	global $concerto;
	return $concerto->DB;
}

/**
 * Pointer to the modulator object
 *
 * @package Concerto
 * @subpackage Helpers
 * @see \application\system\Modulator
 */
function Modulator() {
	global $concerto;
	return $concerto->Modulator;
}

/**
 * Returns the current user
 *
 * @return \application\models\User The current user object
 * @package Concerto
 * @subpackage Helpers
 */
function CurrentUser() {
	global $concerto;
	return $concerto->User;
}


/**
 * Get a variable from the query
 *
 * @see \application\system\Concerto::handleData
 * @param string $varname The name of the variable
 * @return mixed The value of the query variable
 * @package Concerto
 * @subpackage Helpers
 */
function getQueryVar($varname) {
	global $concerto;
	return $concerto->queryVars[$varname];
}

/**
 * Set a variable in the query
 *
 * @see \application\system\Concerto::handleData
 * @param string $varname The name of the variable
 * @param mixed $value The value to set it to
 * @package Concerto
 * @subpackage Helpers
 */
function setQueryVar($varname, $value) {
	global $concerto;
	$concerto->queryVars[$varname] = $value;
}

/**
 * Remove a variable from the query
 * 
 * @see \application\system\Concerto::handleData
 * @param string $varname The name of the variable to delete
 * @package Concerto
 * @subpackage Helpers
 */
function deleteQueryVar($varname) {
	global $concerto;
	unset($concerto->queryVars[$varname]);
}

/**
 * Set a persistent option in the database
 * 
 * @param string $option The name of the option
 * @param string $value The value to set it to.
 * @package Concerto
 * @subpackage Helpers
 */
function setOption($option, $value) {
	global $concerto;
	return $concerto->setOption($option, $value);
}

/**
 * Get a persistent option from the database
 *
 * @param string $option The name of the option
 * @package Concerto
 * @subpackage Helpers
 */
function getOption($option) {
	global $concerto;
	return $concerto->getOption($option);
}

/**
 * Returns the name of a class with the given namespace
 *
 * Just easier than typing all of the backslashes and stuff, I used it
 * for a while but not really anymore
 *
 * @deprecated
 * @param string $namespace The namespace of the class
 * @param string $class The class name
 * @package Concerto
 * @subpackage Helpers
 */
function getClass($namespace, $class) {
	switch($namespace) {
		case 'auth':
			return 'application\\auth\\'.$class;
			break;
		case 'initializers':
			return 'application\\initializers\\'.$class;
			break;
		case 'models':
			return 'application\\models\\'.$class;
			break;
		case 'datatypes':
			return 'application\\models\\datatypes\\'.$class;
			break;
		case 'utilities':
			return 'application\\utilities\\'.$class;
			break;
		case 'views':
			return 'application\\views\\'.$class;
			break;
	}
}

/**
 * Returns the link to the current page
 *
 * Can change the value of a query variable in the URL. Useful
 * for changing pages, for instance
 *
 * @param string $queryVar The name of the query variable you want to change
 * @param string $value The value of the query variable
 * @package Concerto
 * @subpackage Helpers
 */
function currentPageLink($queryVar, $value) {
	parse_str($_SERVER['QUERY_STRING'], $queryString);
	$queryString[$queryVar] = $value;
	
	
	return '?'.http_build_query($queryString);
	
}

/**
 * Give a timed debug report
 *
 * @param $string The message
 * @package Concerto
 * @subpackage Helpers
 */
function reportDebug($string) {
	global $DEV;
	$relativeTime = $DEV->getTimer('dev');
	if(getOption('debug_mode') == TRUE)
		echo '<pre><b>[Debug ('.$relativeTime.')]</b> '.$string.'</pre>';
}

/**
 * Get the title of an object from the database
 * 
 * @param string $resourceType Can be 'tag', 'tag_group', 'content', or 'title'
 * @param int $resourceID The ID of the object
 * @package Concerto
 * @subpackage Helpers
 */
function getTitle($resourceType, $resourceID) {
	switch($resourceType) {
		case 'tag':
		case 'tag_group':
			$title = DB()->getVar("SELECT `tag_title` FROM `cn_tags` WHERE `tag_id`='$resourceID'");
			break;
		case 'content':
			$title = DB()->getVar("SELECT `content_title` FROM `cn_content` WHERE `content_id`='$resourceID'");
			break;
		case 'type':
			$title = DB()->getVar("SELECT `ct_title` FROM `cn_content_types` WHERE `ct_id`='$resourceID'");
			break;
		default:
			\application\utilities\Utility::triggerError('getTitle(): Resource type <b>'.$resourceType.'</b> not recognized.');
			break;
	}
	return $title;
}

/**
 * Displays an image based on given parameters
 *
 * @param string $args URL-formatted query string of arguments. Available arguments are width, height, cropratio, link, and image
 * @param bool $echo Whether to echo the image, or return the HTML as a string
 * @package Concerto
 * @subpackage Helpers
 */
function displayImage($args, $echo = TRUE) {
	if($width = $args['width']) {
		$params[] = "width=".$width;
	}
	if($height = $args['height']) {
		$params[] = "height=".$height;
	}
	if($cropratio = $args['cropratio']) {
		$params[] = "cropratio=".$cropratio;
	}
	if($link = $args['link']) {
		$args['before'] = '<a href="'.$link.'">';
		$args['after'] = "</a>";
	}
	
	$params[] = "image=" . $args['src'];

		
	$params = implode("&amp;", $params);
	
	$image = $args['before'].'<img class="'.$args['class'].'" src="'.CN_URL.'?view=image&'.$params.'" alt="'.$args['alt'].'"/>'.$args['after'];
	
	if($echo) echo $image;
	else return $image;
}

/**
 * Gets a meta data value for a content field
 *
 * @param int $field The ID of the field
 * @param string $key The name of the meta data
 * @package Concerto
 * @subpackage Helpers
 */
function getFieldMetaData($field, $key) {
	return DB()->getVar("SELECT `cfm_value` FROM `cn_field_meta` WHERE `cfm_field`='$field' AND `cfm_key`='$key'");
}

/** 
 * Sets a meta data value for a content field
 *
 * @param int $field The ID of the field
 * @param string $key The name of the meta data
 * @param string $value The value to set it to
 * @package Concerto
 * @subpackage Helpers
 */
function setFieldMetaData($field, $key, $value) {
	if(DB()->getCount("SELECT * FROM `cn_field_meta` WHERE `cfm_key`='$key' AND `cfm_field`='$field'")) {
		DB()->query("UPDATE `cn_field_meta` SET `cfm_value`='$value' WHERE `cfm_key`='$key' AND `cfm_field`='$field' LIMIT 1");
	}
	else {
		DB()->query("INSERT INTO `cn_field_meta` (`cfm_value`, `cfm_key`, `cfm_field`) VALUES('$value', '$key', '$field')");
	}
}

/**
 * Gets the permalink of a resource
 *
 * @see \application\system\Permalink::buildPermalink()
 * @param string $resource The resource type
 * @param int $id The resource ID
 * @package Concerto
 * @subpackage Helpers
 */
function getPermalink($resource, $id) {
	return application\system\Permalink::buildPermalink($resource, $id);
}

/**
 * Set a query var in the URL
 * 
 * Pretty much deprecated I think
 * 
 * @deprecated
 * @param string $var The name of the variable
 * @param string $val The value to set it to
 * @package Concerto
 * @subpackage Helpers
 */
function setUrlQueryVar($var, $val) {
	global $concerto;
	$concerto->urlQueryVars[$var] = $val;
}

/**
 * Gets the current URL, optionally excluding a query variable
 *
 * @param string $exclude Name of a query variable to exclude
 * @return string The URL with optional exclusion
 * @package Concerto
 * @subpackage Helpers
 */
function currentUrl($exclude = null) {
	global $concerto;
	$url = $concerto->UrlInterpreter->currentUrl;
	$pos = strpos($url, '?');
	$url = substr($url, 0, $pos);
	
	$queryString = urlQueryString($exclude);
	
	return $url . '?' . $queryString;
}


/**
 * Returns the query-string portion of the URL, with optional exclusion
 *
 * @param array $exclude Array of variables to exclude
 * @return string The query string generated with http_build_query
 * @package Concerto
 * @subpackage Helpers
 */
function urlQueryString($exclude = null) {
	global $concerto;
	$queries = $concerto->urlQueryVars;
	
	if($exclude) {
		foreach($exclude as $ex) {
			unset($queries[$ex]);
		}
	}
	
	return http_build_query($queries);
}

/**
 * Gets a common file from the includes server
 *
 * @param string $path The relative path from the includes folder
 * @return string The full URL path of the file
 * @package Concerto
 * @subpackage Helpers
 */
function getCommonFile($path) {
	return CN_INCLUDES_URL.$path;
	//return 'http://concerto.tavorinteractive.com/includes/'.$path;
}


 function cleanData($data) {
		// Strip out \o
		$data = str_replace('\o', '', $data);
		
		return $data;
	}
?>